<template>
    <div class="page_1">
        <div class="top_box">
            <!-- <el-input style="width:200px" placeholder="搜索用户信息" v-model="keyword"></el-input>
            <el-select style="width:200px;margin-left:20px" v-model="admin_id" placeholder="请选择管理中心">
                <el-option v-for="item in managementCenter" :key="item.id" :label="item.nickname" :value="item.id">
                </el-option>
            </el-select>
            <el-select style="width:200px;margin-left:20px" v-model="department" placeholder="请选择部门">
                <el-option v-for="item in departmentList" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
            </el-select>
            <el-button type="warning" style="margin-left:20px" icon="el-icon-search" size="small"
                @click="getInfo">搜索</el-button>
            <el-button type="info" style="margin-left:20px" size="small" @click="reset">重置</el-button> -->
            <el-button type="warning" icon="el-icon-plus" size="small" @click="add">添加消息</el-button>
        </div>
        <el-table :data="data" style="width: 100%" border>
            <el-table-column prop="title" label="标题">
            </el-table-column>
            <el-table-column prop="desc" label="消息详情">
            </el-table-column>
            <el-table-column label="消息图片单张">
                <template slot-scope="{row}">
                    <el-image style="width:60px;height:60px" :src="row.img" v-if="row.img" :preview-src-list="[row.img]">
                    </el-image>
                </template>
            </el-table-column>
            <el-table-column label="时间">
                <template slot-scope="{row}">
                    {{ comTime(row.addtime) }}
                </template>
            </el-table-column>
            <!-- <el-table-column label="操作">
                <template slot-scope="{row}">
                    <el-button @click="del">删除</el-button>
                </template>
            </el-table-column> -->
        </el-table>
        <!-- 表单 -->
        <el-dialog :visible.sync="dialogVisible" title="添加" append-to-body width="900px" @close="formReset">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row type="flex" :gutter="10">
                    <el-col :span="12">
                        <el-form-item label="标题" prop="title">
                            <el-input v-model="form.title"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="消息详情" prop="desc">
                            <el-input type="textarea" v-model="form.desc"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="消息图片" prop="img">
                            <el-upload-image v-model="form.img"></el-upload-image>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row type="flex" justify="center">
                    <el-button type="primary" @click="initiate()">发布</el-button>
                    <el-button @click="formReset()" type="primary">重置</el-button>
                </el-row>
            </el-form>
        </el-dialog>
        <el-pagination class="main-pagination" :total="total" :current-page.sync="page" :page-size="limit" layout="prev, pager, next, jumper,total" @current-change="getInfo" hide-on-single-page></el-pagination>
    </div>
</template>

<script>
import {
    get, del, post
} from "@/assets/js/http.js";
export default {
    data() {
        return {
            data: [],
            total: 0,
            limit: 20,
            page: 1,
            dialogVisible: false,
            form: {
                title: "",
                desc: "",
                img: []
            },
            rules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' }
                ],
                desc: [
                    { required: true, message: '请输入消息详情', trigger: 'blur' }
                ]
            },
        }
    },
    components: {
        "el-upload-image": () => import("@/components/el-upload-image.vue")
    },
    methods: {
        getInfo() {
            post({
                url: "/admin/answer.Subject/syslist",
                loading: true
            }).then(res => {
                this.data = res.data
                this.total = res.total
                this.data = res.data
            })
        },
        // 添加
        add() {
            this.dialogVisible = true
            this.formReset()
        },
        // 发布
        initiate() {
            let data = JSON.parse(JSON.stringify(this.form))
            data.img = data.img[0]?.url ? [data.img[0].url] : ""
            post({
                url: "/admin/answer.Subject/syseditPost",
                data,
                loading: true,
                successTip: true,
            }).finally(() => {
                this.dialogVisible = false
                this.getInfo()
            })
        },
        // del() {
        //     this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
        //         confirmButtonText: '确定',
        //         cancelButtonText: '取消',
        //         type: 'warning'
        //     }).then(() => {
        //         post({
        //             url:""
        //         })
        //     }).catch(() => {
        //         this.$message({
        //             type: 'info',
        //             message: '已取消删除'
        //         });
        //     });
        // },
        // 表单关闭回调
        formReset() {
            this.form = Object.assign({}, this.$options.data().form)
        },
    },
    computed: {
        comTime() {
            return (tiem) => {
                const d = new Date(tiem * 1000);
                let nian = d.getFullYear();
                let yue = d.getMonth();
                let ri = d.getDate();
                return `${nian}-${yue + 1}-${ri}`;
            };
        },
    },
    mounted() {
        this.getInfo()
    }
}
</script>

<style></style>